<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Ccdchest extends controller {

	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/activity/cdchest.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$orm    = C\model::load('pro.sng.cdchest');
		$where  = array();
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$orm  = C\model::load('pro.inc.gift');
			foreach ($list as &$item) {
				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
				if (!empty($item['giftids'])) {
					$config= array();
					$gifts = explode(',', $item['giftids']);
					foreach ($gifts as $giftstr) {
						if (!$giftstr || strpos($giftstr, '-') === false) {
							continue;
						}
						list($giftid, $nums) = explode('-', $giftstr);
						$gift     = $orm->get_one($giftid);
						if (!$gift) {
							continue;
						}
						$config[] = $gift['name'].' x '.$nums;
					}
					$item['gifts']= implode(',', $config);
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要删除的记录');
		}
		foreach ($ids as $id) {
			$item  = C\model::load('pro.sng.cdchest')->get_one($id);
			if (!$item) {
				continue;
			}
			if ($item['lv'] >= 1 && $item['lv'] <= 6) {
				$view->assign_error('2', '宝箱等级[1-6]的数值记录不允许删除.');
			}
			C\model::load('pro.sng.cdchest')->delete($id);
		}
		$view->display();
	}

	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::post('id');
		$data  = array();
		$data['lv']     = (int)input::post('lv');
		$data['exp']    = (int)input::post('exp');
		$data['coins']  = (int)input::post('coins');
		$data['cdtime'] = (int)input::post('cdtime');
		$data['giftids']= input::post('giftids');
		if ($data['lv'] < 1 || $data['lv'] > 7) {
			$view->assign_error('1', '宝箱等级数值需在1-6之间');
		}
		if ($data['cdtime'] < 1) {
			$view->assign_error('2', '宝箱CD时间不能小于1');
		}
		//新增的时候判断是否添加过了
		if ($id < 1) {
			$where = array('lv'=>$data['lv']);
			$total = C\model::load('pro.sng.cdchest')->get_total($where);
			if ($total > 0) {
				$view->assign_error('1', '宝箱等级不允许重复');
			}
		}
		if ($data['exp'] < 0 || $data['coins'] < 0) {
			$view->assign_error('2', '经验奖励和星币奖励不能小于0');
		}
		$data['giftids'] = trim($data['giftids']);
		if ($data['giftids']) {
			$orm     = C\model::load('pro.inc.gift');
			$giftids = explode(',', $data['giftids']);
			$gifts   = array();
			foreach ($giftids as $giftstr) {
				//格式不匹配
				if (!$giftstr || strpos($giftstr, '-') == false) {
					continue;
				}
				list($giftid, $nums) = explode('-', $giftstr);
				$gift     = $orm->get_one($giftid);
				if ($nums > 0 && $gift) {
					$gifts[] = $giftstr;
				}
			}
			$data['giftids'] = implode(',', $gifts);
		}
		if ($id > 0) {
			C\model::load('pro.sng.cdchest')->save($id, $data);
		} else {
			C\model::load('pro.sng.cdchest')->new_one($data);
		}
		$view->display();
	}
}